package com.basic.config;

import com.alibaba.fastjson.JSON;
import com.area.entity.Area;
import com.area.entity.Brand;
import com.area.entity.Producttype;
import com.area.service.IAreaService;
import com.area.service.IBrandService;
import com.area.service.IProducttypeService;
import com.code.util.Variable;
import com.productinfo.entity.Vo.ProductInfoVo;
import com.productinfo.service.IProductinfoService;
import com.shopinfo.entity.Shopinfo;
import com.shopinfo.service.IShopinfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class InitCommandListerRunner  implements CommandLineRunner {

    //品牌
    @Autowired
    private IBrandService brandService;
    //分类
    @Autowired
    private IProducttypeService producttypeService;
    // 商品
    @Autowired
    private IProductinfoService productinfoService;
    //区域
    @Autowired
    private IAreaService areaService;
    //商家
    @Autowired
    private IShopinfoService shopinfoService;
    // 引入缓存
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void run(String... args)  {
        //清除所有缓存
        redisTemplate.delete(Variable.BRAND_LIST);
        redisTemplate.delete(Variable.PRODUCT_TYPE_LIST);
        redisTemplate.delete(Variable.PRODUCT_INFO_LIST);
        redisTemplate.delete(Variable.AREA_LIST);
        redisTemplate.delete(Variable.SHOP_INFO_LIST);

        //品牌写入缓存
        List<Brand> brands =  brandService.queryBrandAll(new Brand());
//        redisTemplate.opsForValue().set("brandList",brands);
        redisTemplate.opsForValue().set(Variable.BRAND_LIST, brands);
        log.info(String.format("品牌同步缓存数据= %d,条",brands.size()));
        //分类写入缓存
        List<Producttype> producttypes = producttypeService.queryProductTypeAll(new Producttype());
//        redisTemplate.opsForValue().set("productTypeList",producttypes);
        redisTemplate.opsForValue().set(Variable.PRODUCT_TYPE_LIST, producttypes);
        log.info(String.format("分类同步缓存数据= %d,条",producttypes.size()));
        //商品写入缓存
        List<ProductInfoVo> productinfos = productinfoService.queryProductInfoAll();
//        redisTemplate.opsForValue().set("productInfoList",productinfos);
        redisTemplate.opsForValue().set(Variable.PRODUCT_INFO_LIST, productinfos);
        log.info(String.format("商品同步缓存数据= %d,条",productinfos.size()));
        //区块写入
        List<Area> areas = areaService.queryBasicAreaAll();
//        redisTemplate.opsForValue().set("areaList",areas);
        redisTemplate.opsForValue().set(Variable.AREA_LIST, areas);
        log.info(String.format("地区同步缓存数据= %d,条",areas.size()));
        //商家写入
        List<Shopinfo> shopinfos = shopinfoService.queryShopinfo();
//        redisTemplate.opsForValue().set("shopinfoList",shopinfos);
        redisTemplate.opsForValue().set(Variable.SHOP_INFO_LIST, shopinfos);
        log.info(String.format("商家同步缓存数据= %d,条",shopinfos.size()));
        log.info("同步时间..." +new Date());
    }
}
